#ifndef BVH_h
#define BVH_h

#include "BBox.h"
#include <vector>
#include <stdint.h>
#include "Object.h"
#include "Frustum.h"
#include <fstream>

using std::ofstream;
//#define DEBUG

typedef unsigned __int64 ETYPE;

//! Node descriptor for the flattened tree
struct BVHFlatNode {
  BBox bbox;
  uint32_t start, nPrims, rightOffset;
};

//! \author Brandon Pelfrey
//! A Bounding Volume Hierarchy system for fast Ray-Object intersection tests
class BVH {
	friend class Frustum;
private:
	uint32_t nNodes, nLeafs, leafSize;
	std::vector<Object*>* build_prims;
#ifndef DEBUG
	ETYPE* bitmap;
#else
	bool bitmap[100];
#endif
	int bLen;
	//! Build the BVH tree out of build_prims
	void build();

	// Fast Traversal System
	BVHFlatNode *flatTree;
	void setBitmap(int number);
	bool getBitmap(int number) const;
	void clearBitmap();
	
  public:
	  BVH(std::vector<Object*>* objects, uint32_t leafSize=4);
	  void FrustumCulling(const Frustum* frustum);
	  ~BVH();
};

#endif
